/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PickCustomerDialog.java
 *
 * Created on 09-01-2011, 17:26:40
 */
package UILayer;

import ControlLayer.ActorController;
import ControlLayer.GlobalInstances;
import ModelLayer.Actor;
import ModelLayer.Role;
import UILayer.Screens.createCustomerUI;
import java.io.InvalidObjectException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Moulde
 */
public class PickCustomerDialog extends javax.swing.JDialog
{
    ActorController actorController;
    MainGuiView mainView;

    /** Creates new form PickCustomerDialog */
    public PickCustomerDialog(java.awt.Frame parent, boolean modal, MainGuiView mainView)
    {
        super(parent, modal);
        initComponents();
        this.mainView = mainView;

        actorController = new ActorController();
        ArrayList<Actor> customers = actorController.getActorsByRole(Role.Customer);

        searchList1.setItems(customers);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        searchList1 = new SwingExtensions.SearchList();
        lblStatus = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Vælg");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        searchList1.setName("searchList1"); // NOI18N
        searchList1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                searchList1KeyPressed(evt);
            }
        });

        lblStatus.setName("lblStatus"); // NOI18N

        jButton2.setText("Opret");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchList1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblStatus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 203, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchList1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(lblStatus)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
        SetSelectedCustomer();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void SetSelectedCustomer()
    {
        Object selectedItem = searchList1.GetSelectedItem();
        if (selectedItem != null)
        {
            try
            {
                GlobalInstances.getInstance().setCurrentCustomer((Actor) searchList1.GetSelectedItem());
                this.setVisible(false);
            }
            catch (InvalidObjectException ex)
            {
                Logger.getLogger(PickCustomerDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else
        {
            lblStatus.setText("Du skal vælge en kunde, eller oprette en ny");
        }
    }

    private void searchList1KeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_searchList1KeyPressed
    {//GEN-HEADEREND:event_searchList1KeyPressed
        if (evt.getKeyCode() == 10)
        {
            SetSelectedCustomer();
        }
    }//GEN-LAST:event_searchList1KeyPressed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton2ActionPerformed
    {//GEN-HEADEREND:event_jButton2ActionPerformed
        this.setVisible(false);
        mainView.AddPane(new createCustomerUI(), "Opret kunde");
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel lblStatus;
    private SwingExtensions.SearchList searchList1;
    // End of variables declaration//GEN-END:variables
}
